<template>
  <!-- 团队案件池-批量还款弹窗 -->
  <section class="component team-case-batch-repayment" v-loading="loading.state">
    <data-box :data="selectCaseList">
      <template slot="columns">
        <el-table-column prop="caseNumber" label="案件编号"></el-table-column>
        <el-table-column prop="personalName" label="客户姓名"></el-table-column>
        <el-table-column prop="overdueAmount" label="欠款金额"></el-table-column>
        <el-table-column prop label="还款金额">
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.repaymentAmount"
              type="number"
              :min="0"
              :precision="0"
              :max="scope.row.overdueAmount"
              @blur="inputMax(scope)"
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column prop label="操作">
          <template slot-scope="scope">
            <el-button
              @click.native.prevent="deleteClick(scope.$index, selectCaseList)"
              type="text"
            >删除</el-button>
          </template>
        </el-table-column>
      </template>
    </data-box>
    <el-form class="btns">
      <el-form-item label-width="0px" align="center">
        <el-button @click="close">取消</el-button>
        <el-button @click="ConfirmBtn">确定</el-button>
      </el-form-item>
    </el-form>
  </section>
</template>
<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { Dependencies } from "~/core/decorator";
import { Emit, Prop, Watch } from "vue-property-decorator";
import { Getter } from "vuex-class";
import DataForm from "~/components/common/data-form.vue";
import DataBox from "~/components/common/data-box.vue";
import { CaseRepaymentService } from "~/services/domain-service/case-repayment.service";

@Component({
  components: {
    DataForm,
    DataBox
  }
})
export default class TeamCaseBatchRepayment extends Vue {
  @Dependencies(CaseRepaymentService)
  private caseRepaymentService: CaseRepaymentService;
  private loading: any = {
    state: false
  };
  private selectCaseList: any = [];

  @Emit("close")
  private close() {}
  /**
   * 反显数据
   */
  private updateList(data: any) {
    // if (!data.id) return;
    this.selectCaseList=data.map(v=>{
      return {
        id:v.id,
        caseNumber:v.caseNumber,
        personalName:v.personalName,
        overdueAmount:v.overdueAmount,
        repaymentAmount:0
      }
    })
  }
  /**
   * 删除按钮
   */
  private deleteClick(index, rows) {
    rows.splice(index, 1);
  }
  private inputMax(scope) {
    if (scope.row.repaymentAmount > scope.row.overdueAmount) {
      scope.row.repaymentAmount = scope.row.overdueAmount;
    }
    if(scope.row.repaymentAmount<0||scope.row.repaymentAmount===''){
      scope.row.repaymentAmount=0;
    }
  }

  /**
   * 确定按钮
   */
  private ConfirmBtn() {
    if(this.selectCaseList.find(v=>v.repaymentAmount===0)){
      this.$message('还款金额必须大于0')
      return
    }
    const modelData: any = this.selectCaseList.map(v => {
      return {
        caseId: v.id,
        repayAmount: v.repaymentAmount
      };
    });
    this.caseRepaymentService
      .addCaseRepaymentBatch(modelData)
      .subscribe(data => {
        this.$message.success("批量还款成功");
        this.close();
      });
  }
}
</script>

<style lang="less" scoped>
.btns {
  margin-top: 30px;
}
</style>